// pages/my/loging/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    password: '',
    serverHost: app.config.serverHost,
    err: ''
  },
  getPhoneValue(e) {
    this.setData({
      phone: e.detail
    })
  },
  checkPhone() {
    if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
      this.setData({
        err: '请输入正确的手机号码'
      })
    } else {
      this.setData({
        err: ''
      })
    }
  },
  getPasswordValue(e) {
    this.setData({
      password: e.detail
    })
  },
  login () {
    let _this = this
    if (!this.data.phone || !this.data.password) {
      wx.showToast({
        title: '请完成信息',
        images: '../../../static/icons/err.png',
        duration: 2000,
      })
      return
    }
    var model = {
      account_id: this.data.phone,
      password: this.data.password
    }
    app.api.login(model,(res)=>{
      console.log(res)
      if(res.data.status == 422){
        wx.showToast({
          title: res.data.msg,
          image: '../../../static/icons/err.png',
          duration: 2000,
        })
      }else{
        wx.showToast({
          title: '登录成功',
          icon: 'success'
        })
        if(res.data.shenfen=='2'){
          wx.setStorageSync('phone', _this.data.phone)
          wx.redirectTo({
            url: '../../admin/Goodslist/index',
          })
        }else if(res.data.shenfen=='1'){
          wx.setStorageSync('token', res.data.token)
          wx.setStorageSync('phone', _this.data.phone)
          wx.setStorageSync('shopping_cart', res.data.shopping_cart)
          wx.setStorageSync('openid', res.data.openid)
          if(res.data.service == undefined){
            wx.redirectTo({
              url: '../../location/location'
            })
          }else{
            wx.setStorageSync('service',res.data.service.local)
            wx.switchTab({
              url: '../../index/index'
            })
          }
        }else if(res.data.shenfen == '0'){
          wx.setStorageSync('token', res.data.token)
          wx.redirectTo({
            url: '../../superpages/service/service',
          })
        } else if (res.data.shenfen == '3') {
          wx.setStorageSync('token', res.data.token)
          wx.setStorageSync('local', res.data.local)
          wx.setStorageSync('openid', res.data.openid);  
          wx.redirectTo({
            url: '../../service/index',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!wx.getStorageSync('userInfo')) {
      this.setData({
        show: true
      })
    }
  },
  
  getUserInfo(userInfo) {
    if (!userInfo.detail.rawData || !userInfo.detail.signature) {
      wx.showModal({
        title: '提示',
        content: '你没有授权,不能享受菜多多的贴♥服务哦'
      })
      return
    }
    wx.setStorageSync('userInfo', userInfo.detail.rawData)
    this.setData({
      show: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})